﻿package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.text.TextField;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.shadematerials.EnvMapMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	
	/*
	 * coded by moka 2009 / 06/ 20 
	 * name : moka's LightRender
	 */
	
	public class Main extends Sprite 
	{		
		private var scene:Scene3D;
		private var camera:Camera3D;
		private var viewport:Viewport3D;
		private var render:BasicRenderEngine;
		private var bmp:Bitmap;
		private var cube:Cube;
		private var step:uint;
		private var info:TextField;
		private var infoArray:Array;
		private var shp:Shape;
		private var lightMap:BitmapData;
		
		// 貼圖的紋理
		[Embed(source = 'moka.gif')]
		private var EmbeddedImage:Class;
		private var image:Bitmap;		

		public function Main():void 
		{		
			// Class 類別隱式轉換成 Bitmap 類別
			image = new EmbeddedImage(); 
			
			// 設定場景不變形
			stage.scaleMode = "noScale"; 
			
			// Scene3D 用來組織場景母子關係樹狀架構
 			scene = new Scene3D();

			// Camera3D 為 3D 場景可視範圍設定
			camera = new Camera3D();
			
			// Viewport3D 用在呈現於 flash 場景(其類別繼承Sprite)
			viewport = new Viewport3D(640, 480);
			
			// 用以渲染 3D 場景之類別
			render = new BasicRenderEngine();
			
			// 繪製 viewport 用
			bmp = addChild(new Bitmap(new BitmapData(640, 480, true, 0xFF000000))) as Bitmap; 
			
			// 光源材質製作用
			lightMap = new BitmapData(640, 480, true, 0);
			shp = new Shape();
			
			
			// 建立球體並改變球體網格
			cube = scene.addChild(new Cube(new MaterialsList( { all:new BitmapMaterial(image.bitmapData,true)}),500,500,500))as Cube;
			cube.rotationX = 30;
			cube.rotationY = 30;
			cube.rotationZ = 30;
			
			// 註冊事件
			stage.addEventListener(Event.ENTER_FRAME, onProcess);	

			// Moka's work 2009
			(addChild(new TextField()) as TextField).htmlText = "<font color='#FFFFFF'>Moka's work 2009</font>"; 
		}		
		
		private function onProcess(e:Event):void {	
			cube.rotationY++;
			
			// 光源繪製與點陣化
			shp.graphics.beginGradientFill("radial", [0xFFCC00, 0x663300], [1, 1],[0,120],new Matrix(1, 0, 0, 1, mouseX, 480-mouseY));
			shp.graphics.drawRect(0, 0, 640, 480);
			shp.graphics.endFill();
			lightMap.draw(shp);
			
			var bmpBuffer:BitmapData = bmp.bitmapData.clone();	
			
			// 材質混合部分
			
			// 第一層材質以 BitmapMaterial繪製
			bmpBuffer.fillRect(bmp.bitmapData.rect, 0xFF000000);			
			cube.material = new BitmapMaterial(image.bitmapData, true);
			render.renderScene(scene, camera, viewport);
			bmpBuffer.draw(viewport);			
			
			// 第二層材質以 EnvMapMaterial 環境貼圖 add 的疊合方式繪製
			cube.material = new EnvMapMaterial(new PointLight3D, lightMap);
			render.renderScene(scene, camera, viewport);			
			bmpBuffer.draw(viewport, new Matrix, new ColorTransform, "add");
			
			bmp.bitmapData.draw(bmpBuffer,new Matrix, new ColorTransform);
		}   
	}	
}